<?php
include 'conexao.php';
session_start();
?>

<!DOCTYPE html>
<html lang="pt">
<head>
    <meta charset="UTF-8">
    <title>Relatório de Movimentos Diários</title>
    <link rel="stylesheet" href="estilo.css"> <!-- Seu CSS aqui -->
    <style>
        body { font-family: Arial, sans-serif; background: #f5f5f5; margin: 0; padding: 20px; }
        h2 { text-align: center; margin-bottom: 20px; }
        table { width: 100%; border-collapse: collapse; background: white; }
        th, td { border: 1px solid #ccc; padding: 8px; text-align: center; }
        th { background: #007bff; color: white; }
        tr:nth-child(even) { background-color: #f9f9f9; }
        .saldo-final { font-weight: bold; font-size: 18px; margin-top: 20px; text-align: right; }
    </style>
</head>
<body>

<h2>Relatório de Movimentos Diários</h2>

<table>
    <thead>
        <tr>
            <th>Data</th>
            <th>Descrição</th>
            <th>Tipo</th>
            <th>Valor (Kz)</th>
        </tr>
    </thead>
    <tbody>
        <?php
        $query = "SELECT * FROM movimentos_diarios ORDER BY data ASC";
        $result = $conn->query($query);

        $saldo = 0;
        $total_entrada = 0;
        $total_saida = 0;

        if ($result->num_rows > 0) {
            while ($mov = $result->fetch_assoc()) {
                $valor = number_format($mov['valor'], 2, ',', '.');
                echo "<tr>";
                echo "<td>" . date('d/m/Y', strtotime($mov['data'])) . "</td>";
                echo "<td>" . $mov['descricao'] . "</td>";
                echo "<td>" . ucfirst($mov['tipo']) . "</td>";
                echo "<td>" . $valor . "</td>";
                echo "</tr>";

                if ($mov['tipo'] == 'entrada') {
                    $saldo += $mov['valor'];
                    $total_entrada += $mov['valor'];
                } else {
                    $saldo -= $mov['valor'];
                    $total_saida += $mov['valor'];
                }
            }
        } else {
            echo "<tr><td colspan='4'>Nenhum movimento registrado.</td></tr>";
        }
        ?>
    </tbody>
</table>

<div class="saldo-final">
    <p>Total de Entradas: <strong><?= number_format($total_entrada, 2, ',', '.') ?> Kz</strong></p>
    <p>Total de Saídas: <strong><?= number_format($total_saida, 2, ',', '.') ?> Kz</strong></p>
    <p>Saldo Atual: <strong><?= number_format($saldo, 2, ',', '.') ?> Kz</strong></p>
</div>

</body>
</html>
